import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage'

Vue.use(Vuex)

const USER_KEY = 'toutiao-user'

export default new Vuex.Store({
  state: {
    user: getItem(USER_KEY), // 用户信息
    cachePages: ['LayOutIndex'],  // 需要缓存的路由
  },
  getters: {
  },
  mutations: {
    SETUSER(state, data) {
      state.user = data
      setItem(USER_KEY, data)
    },
    ADDCHACHEPAGE(state, page) {
      if (state.cachePages.indexOf[page] === -1)
        state.cachePages.push(page)
    },
    REMOVECACHEPAGE(state, page) {
      const index = state.cachePages.indexOf[page]
      if (index !== -1) state.cachePages.splice(index, 1)
    }
  },
  actions: {
    // 设置用户信息
    setUser({ commit }, data) {
      commit('SETUSER', data)
    }
  },
  modules: {
  }
})
